In [1]:
def simple_function(x):
print x + 1
simple_function(2)
Note that our function might not work for everything we pass in (you should get an error on this one):
In [2]:
simple_function('2')
Functions can take any number of arguments
In [3]:
def less_simple(a, b, c):
print a + b + c
less_simple(1, 2, 3)
And now we can pass in strings (as long as they're all strings):
In [4]:
less_simple('These ', 'should ', 'concatenate')
You can also use named arguments instead of positional ones. Note how the printed order is still "abc" even though we pass in "acb."
In [5]:
less_simple(a='first', c=' third', b=' second')
In [14]:
def printfun(x):
print x
def retfun(x):
return x
In [16]:
printfun('something')
In [17]:
retfun('something')
Out[17]:
But notice that in once case we see Out[#]:! In this case, the "print" part of the REPL is displaying the value that was returned. Try this in a script - you shouldn't see anything printed for return statements there!
Also - there's a difference in control flow.
In [18]:
def dumbfun(x):
return x
print 'This will never print :('
In [19]:
dumbfun('something')
Out[19]:
When you execute a previously-defined function, like simple_function(3), we say that you "called" the function. When you call a function, a temporary workspace is set up that will be destroyed when the function returns by:
return statementIn this temporary environment, the variables in the parameter list (in parentheses in the definition) are set to the values passed in. For example, in simple_function(3), x gets set to 3. Afterwards, you can't access these variables!
In [6]:
# Check out the def of simple_function above - we're setting x to 3!
simple_function(3)
# x is no longer defined because simple_function returned (i.e., finished)!
x
Things can get confusing when you use the same names for variables both inside and outside a function. Check out this example:
In [7]:
night = 'night'
day = 'day'
# If you were just reading through, it would be easy to think
# that 'night' in this function corresponds to 'night' above!
def confused_by_names(night, day):
print 'night is', night
print 'day is', day
confused_by_names(day, night)
So, to avoid confusion, use different variable names in every context!
In general, Python has very rich options for defining and calling functions. But we're already getting ahead of Codecademy, so we'll stop here for now.
Python has an extensive standard library that is always included with the python interpreter. You can read about it here:
http://docs.python.org/2/library/
It can seem overwhelming! I've been using it for years and still don't know everything in there!
Another very useful module is pandas. It provides a DataFrame structure that's a lot like a spreadsheet (don't worry - we'll explain what those {} and [] expressions are later):
In [8]:
import pandas
df = pandas.DataFrame({'Day': [1, 2, 3, 4], 'Score': [88, 90, 76, 43]})
The IPython notebook will print these very nicely:
In [9]:
df
Out[9]:
We can efficiently compute information about the table:
In [10]:
df.mean()
Out[10]:
And we can select columns or rows or both (again, we'll explain some of this syntax later):
In [11]:
df.Score # This only works if the column is a valid python variable name!
Out[11]:
In [12]:
df.iloc[1]
Out[12]:
In [13]:
df.Score[1]
Out[13]:
Pandas is very powerful. Even this one library will take time to master. You can read more about it here: